<template>
  <div class="app-container">
    <!--todo 条件查询 -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="检测人" prop="qualityName">
        <el-input v-model="queryParams.qualityName" placeholder="请输入检测人" clearable style="width: 240px"/>
      </el-form-item>

      <el-form-item label="检测结果" prop="qualityResult">
        <el-select
          v-model="queryParams.qualityResult"
          placeholder="请选择检测结果"
          clearable
          style="width: 240px">
          <el-option label="合格" value="0"></el-option>
          <el-option label="不合格" value="1"></el-option>
          <el-option label="待质检" value="2"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="批次查询">
        <el-select v-model="queryParams.productId" placeholder="请选择">
          <el-option v-for="item in prodslist" :key="item.productId" :label="item.batchNum" :value="item.productId"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="作物名查询">
        <el-select v-model="queryParams.productId" placeholder="请选择">
          <el-option v-for="item in productList" :key="item.productId" :label="item.productName" :value="item.productId"></el-option>
        </el-select>
      </el-form-item>

<!--      <el-form-item label="加工厂商名称查询">-->
<!--        <el-select v-model="queryParams.firmId" placeholder="请选择">-->
<!--          <el-option v-for="item in firmList" :key="item.firmId" :label="item.firmName" :value="item.firmId"></el-option>-->
<!--        </el-select>-->
<!--      </el-form-item>-->

<!--      <el-form-item label="司机名称查询">-->
<!--        <el-select v-model="queryParams.driverId" placeholder="请选择">-->
<!--          <el-option v-for="item in driverList" :key="item.driverId" :label="item.name" :value="item.driverId"></el-option>-->
<!--        </el-select>-->
<!--      </el-form-item>-->

<!--      <el-form-item label="物料名称查询">-->
<!--        <el-select v-model="queryParams.goodsMaterialId" placeholder="请选择">-->
<!--          <el-option v-for="item in goodsMaterialList" :key="item.goodsMaterialId" :label="item.materialName" :value="item.goodsMaterialId"></el-option>-->
<!--        </el-select>-->
<!--      </el-form-item>-->

      <el-form-item label="检测日期">
        <el-date-picker
          v-model="qualityTimeRange"
          style="width: 240px"
          value-format="yyyy-MM-dd HH:mm:ss"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期">
        </el-date-picker>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <!--todo 按钮 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                   v-hasPermi="['ewem:quality:add']">新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
                   v-hasPermi="['ewem:quality:export']">导出
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-upload class="upload-demo" action="http://localhost:9202/quality/importUploads" multiple :limit="3">
          <el-button type="primary" plain size="small" slot="trigger">导入</el-button>
        </el-upload>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <!--todo 列表 -->
    <el-table v-loading="loading" :data="QualityList">
      <el-table-column label="质检id" prop="qualityId"/>
      <el-table-column label="批次" prop="batchNum"/>
      <el-table-column label="作物名称" prop="productName" :visible.sync="prodopen">
        <template slot-scope="scope">
          <el-button type="text" @click="prodOpen(scope.row.productId)">{{ scope.row.productName }}</el-button>
        </template>
      </el-table-column>
      <el-table-column label="检测日期" prop="qualityTime"/>
      <el-table-column label="检测人" prop="qualityName"/>
      <el-table-column label="图片" prop="avatar">
        <template slot-scope="scope">
          <el-image :src="scope.row.avatar" style="width: 80px;height: 80px;"></el-image>
        </template>
      </el-table-column>
      <el-table-column label="检测结果" prop="qualityResult">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.qualityResult==0">合格</el-tag>
          <el-tag v-if="scope.row.qualityResult==1">不合格</el-tag>
          <el-tag v-if="scope.row.qualityResult==2">待质检</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="创建日期" prop="createTime"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                     v-hasPermi="['ewem:quality:updates']">修改
          </el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                     v-hasPermi="['ewem:quality:dels']">删除
          </el-button>
          <br>
          <el-button size="mini" type="text" icon="el-icon-zhijian" @click="handleZhijian(scope.row)">审核</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--todo 分页 -->
    <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList"/>

    <!--todo 添加或修改角色配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" label-width="100px">
        <el-form-item label="批次查询">
          <el-select v-model="form.productId" placeholder="请选择" :disabled="disa">
            <el-option v-for="item in prodslist" :key="item.productId" :label="item.batchNum" :value="item.productId"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="作物名">
          <el-select v-model="form.productId" placeholder="请选择作物">
            <el-option v-for="item in productList" :key="item.productId" :label="item.productName" :value="item.productId"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="质检图片" :label-width="formLabelWidth">
          <el-upload
            class="avatar-uploader"
            action="http://localhost:9202/quality/upload"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload">
            <img v-if="imageUrl" :src="imageUrl" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入备注内容"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>

    <!--todo 作物该行的数据详情-->
    <el-dialog :title="prodTitle" :visible.sync="prodopen" width="500px" append-to-body>
      <el-form :model="forms">
        <el-form-item label="农作编码" :label-width="formLabelWidth">
          <span style="color: #8c939d">{{this.forms.productNum}}</span>
        </el-form-item>
        <el-form-item label="农作名称" :label-width="formLabelWidth">
          <span style="color: #8c939d">{{this.forms.productName}}</span>
        </el-form-item>
        <el-form-item label="肥料名称" :label-width="formLabelWidth">
          <span style="color: #8c939d">{{this.forms.manure}}</span>
        </el-form-item>
        <el-form-item label="种植方式" :label-width="formLabelWidth">
          <span style="color: #8c939d">{{this.forms.modeName}}</span>
        </el-form-item>
        <el-form-item label="种植年度" :label-width="formLabelWidth">
          <span style="color: #8c939d">{{this.forms.years}}</span>
        </el-form-item>
      </el-form>
    </el-dialog>

    <!--todo 加工厂商该行的数据详情-->
    <el-dialog :title="firmTitle" :visible.sync="firmopen" width="500px" append-to-body>
      <el-form :model="firmform" label-width="100px">
        <el-form-item label="工厂名称" :label-width="formLabelWidth">
          <span style="color: #8c939d">{{this.firmform.firmName}}</span>
        </el-form-item>
        <el-form-item label="工厂负责人" :label-width="formLabelWidth">
          <span style="color: #8c939d">{{this.firmform.firmPrincipal}}</span>
        </el-form-item>
        <el-form-item label="作物名称" :label-width="formLabelWidth">
          <span style="color: #8c939d">{{this.firmform.productName}}</span>
        </el-form-item>
        <el-form-item label="加工数量" :label-width="formLabelWidth">
          <span style="color: #8c939d">{{this.firmform.num}}</span>
        </el-form-item>
      </el-form>
    </el-dialog>

    <!--todo 司机该行的数据详情-->
    <el-dialog :title="driverTitle" :visible.sync="driveropen" width="500px" append-to-body>
      <el-form :model="driverform" label-width="100px">
        <el-form-item label="司机名称" :label-width="formLabelWidth">
          <span style="color: #8c939d">{{this.driverform.name}}</span>
        </el-form-item>
        <el-form-item label="司机电话" :label-width="formLabelWidth">
          <span style="color: #8c939d">{{this.driverform.phone}}</span>
        </el-form-item>
        <el-form-item label="驾驶证类型" :label-width="formLabelWidth">
          <span style="color: #8c939d">{{this.driverform.driverLicenseType}}</span>
        </el-form-item>
      </el-form>
    </el-dialog>

    <!--todo 物料该行的数据详情-->
    <el-dialog :title="goodsTitle" :visible.sync="goodsopen" width="500px" append-to-body>
      <el-form :model="goodsform" label-width="100px">
        <el-form-item label="物料编码" :label-width="formLabelWidth">
          <span style="color: #8c939d">{{this.goodsform.materialCode}}</span>
        </el-form-item>
        <el-form-item label="物料名称" :label-width="formLabelWidth">
          <span style="color: #8c939d">{{this.goodsform.materialName}}</span>
        </el-form-item>
        <el-form-item label="物料数量" :label-width="formLabelWidth">
          <span style="color: #8c939d">{{this.goodsform.materialNum}}</span>
        </el-form-item>
        <el-form-item label="归属部门" :label-width="formLabelWidth">
          <span style="color: #8c939d">{{this.goodsform.deptName}}</span>
        </el-form-item>
      </el-form>
    </el-dialog>

    <!--todo 审核-->
    <el-dialog :title="zhiTitle" :visible.sync="zhijianOpen" width="500px" append-to-body>
      <el-form :model="zhiForm" label-width="100px">
        <el-form-item label="批次查询">
          <el-select v-model="zhiForm.productId" placeholder="请选择" :disabled="disa">
            <el-option v-for="item in prodslist" :key="item.productId" :label="item.batchNum" :value="item.productId"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="作物名">
          <el-select v-model="zhiForm.productId" placeholder="请选择作物" :disabled="disa">
            <el-option v-for="item in productList" :key="item.productId" :label="item.productName" :value="item.productId"></el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="检测结果">
          <template>
            <el-radio v-model="zhiForm.qualityResult" label="0">合格</el-radio>
            <el-radio v-model="zhiForm.qualityResult" label="1">不合格</el-radio>
          </template>
        </el-form-item>
        <el-form-item label="质检图片" :label-width="formLabelWidth" :disabled="disa">
          <el-upload
            class="avatar-uploader"
            action="http://localhost:9202/quality/upload"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload">
            <img v-if="imageUrl" :src="imageUrl" class="avatar">
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
            <div class="el-upload__tip" slot="tip">只能上传jpg/png文件，且不超过500kb</div>
          </el-upload>
        </el-form-item>
        <el-form-item label="缘由">
          <el-input v-model="zhiForm.remark" type="textarea" placeholder="请输入备注内容"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitsForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

import {
  addQuality,
  delQuality,
  driversList,
  firmList, goodsMaterialList,
  listQuality,
  productList,
  updateQuality,
  getProduct, getFirm, getDricer, getGoods, prodsList, updatesQuality
} from "@/api/ewem/quality";

export default {
  data() {
    return {
      procslist:[],
      disa:false,
      zhiForm:{},
      goodsform:{},
      driverform:{},
      firmform:{},
      forms:{},
      prodslist:[],
      prodList: [],
      goodsMaterialList: [],
      driverList: [],
      firmList: [],
      productList: [],
      formLabelWidth: '120',
      imageUrl: '',
      QualityList: [],
      productionTimeRange: [],
      qualityTimeRange: [],
      dateRange: [],
      // 遮罩层
      loading: true,
      ids: [],
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 弹出层标题
      title: "",
      prodTitle: "",
      firmTitle: "",
      driverTitle: "",
      goodsTitle: "",
      zhiTitle:"",
      // 是否显示弹出层
      open: false,
      prodopen: false,
      firmopen: false,
      driveropen: false,
      goodsopen: false,
      zhijianOpen:false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 3,
      },
      // 表单参数
      form: {},
      defaultProps: {
        children: "children",
        label: "label"
      },
      productName: '',
      firmName: '',
      driversName: '',
      materialName: ''
    };
  },
  created() {
    this.getList()
    this.getProdList()
    this.getFirmList()
    this.getprodslist()
    this.getproclist()
    this.getDriversList()
    this.getGoodsMaterialList()
  },
  methods: {
    getproclist(){
      prodsList().then(response=>{
        this.procslist = response.data
      })
    },
    getprodslist(){
      prodsList().then(response=>{
        this.prodslist = response.rows
      })
    },
    getProdList() {
      productList().then(response => {
        this.productList = response.rows
      })
    },
    getFirmList() {
      firmList().then(response => {
        this.firmList = response.rows
      })
    },
    getDriversList() {
      driversList().then(response => {
        this.driverList = response.rows
      })
    },
    getGoodsMaterialList() {
      goodsMaterialList().then(response => {
        this.goodsMaterialList = response.rows
      })
    },
    getList() {
      this.loading = true;
      listQuality(this.addDateRange(this.queryParams, this.qualityTimeRange)).then(response => {
          this.QualityList = response.rows;
          this.total = response.total;
          this.loading = false;
        }
      );
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    // 表单重置
    reset() {
      this.queryParams = {
        qualityId: undefined,
        qualityName: undefined,
        batchNum: undefined,
        productName: undefined,
        firmName: undefined,
        driversName: undefined,
        materialName: undefined,
        productionTime: undefined,
        qualityTime: undefined,
        remark: undefined,
        avatar: undefined,
        qualityResult: undefined,
        createTime: undefined
      }
      this.resetForm("queryForm");
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.reset()
      // this.queryParams={}
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.form = {}
      this.open = true;
      this.title = "添加质检表";
      this.disa=false
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.open = true
      this.imageUrl = row.avatar
      this.form = row
      this.title = '修改质检表'
      this.disa=true;
    },
    /** 提交按钮 */
    submitForm() {
      if (this.form.qualityId != undefined) {
        updateQuality(this.form).then(response => {
          this.$modal.msgSuccess("修改成功");
          this.open = false;
          this.getList();
        });
      } else {
        addQuality(this.form).then(response => {
          this.$modal.msgSuccess("新增成功");
          this.open = false;
          this.getList();
        });
      }
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const qualityIds = row.qualityId || this.ids;
      this.$modal.confirm('是否确认删除角色编号为"' + qualityIds + '"的数据项？').then(function () {
        return delQuality(qualityIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('ewem/quality/export', {
        ...this.queryParams
      }, `质检.xlsx`)
    },
    /** 头像 */
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw);
      this.form.avatar = res;
      if (res.code) {
        this.$message.success("上传成功")
      } else {
        this.$message.error("上传失败")
      }
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg';
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      return isJPG && isLt2M;
    },
    prodOpen(productId) {
      getProduct(productId).then(response => {
        this.prodopen = true
        this.prodTitle = '农作物详情'
        this.forms = response.data;
        console.log(this.prodList)
      })
    },
    firmOpen(firmId) {
      getFirm(firmId).then(response=>{
        this.firmopen = true
        this.firmTitle = '工厂详情'
        this.firmform = response.data
      })

    },
    driversOpen(driverId) {
      getDricer(driverId).then(response=>{
        this.driveropen = true
        this.driverTitle = '司机详情'
        this.driverform = response.data
      })

    },
    materialOpen(goodsMaterialId) {
      getGoods(goodsMaterialId).then(response=>{
        this.goodsopen = true
        this.goodsTitle = '物料详情'
        this.goodsform = response.data
      })
    },
    handleZhijian(row){
      this.zhijianOpen = true
      this.zhiForm = row
      this.zhiTitle = "审核"
      this.imageUrl = row.avatar
      this.disa=true;
    },
    submitsForm(){
      updatesQuality(this.zhiForm).then(response=>{
        this.zhijianOpen = false
        this.getList()
      })
    }
  }
};
</script>

<style scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
